Page({
	data: {
		showFloat: false,
		showInfo: false,
		balance: '',
		list: [],
		urlAddress: getApp().globalData.imageUrl,
		imageUrl: getApp().globalData.imageUrl,
		checkid: '',
		promLabel: '',
		shopName: '',
		activityId: '',
		rechargeAmount: 0
	},
	onLoad: function(options) {
		// 生命周期函数--监听页面加载
		console.log(options.balance)
		this.setData({
			balance: options.balance,
			activityId: options.id
		})
		this.getActivityinfo(options.id)

	},
	getActivityinfo: function(id) {
		var _self = this;
		wx.request({
			url: getApp().globalData.baseUrl + '/applet/recharge/getActivityDetail.do',
			data: {
				activityCode: id
			},
			header: getApp().globalData.header,
			success: function(res) {
				wx.hideLoading();
				console.log(res)
				if (res.data.code == 200) {
					for (var i = 0; i < res.data.obj.activityItemVos.length; i++) {
						res.data.obj.activityItemVos[i].isSelect = 'nomal';
					}
					res.data.obj.activityItemVos[0].isSelect = 'select';

					_self.setData({
						list: res.data.obj.activityItemVos,
						checkid: res.data.obj.activityItemVos[0].itemId,
						rechargeAmount: res.data.obj.activityItemVos[0].rechargeCondition
					})
				} else {
					wx.redirectTo({
						url: '/pages/recharge/recharge?balance=' + _self.data.balance
					})
				}

			}
		})

	},
	getRechargeInfo: function() {
		var _self = this;
		wx.request({
			url: getApp().globalData.baseUrl + '/applet/recharge/getRechargeHint.do',
			data: {
				itemId: this.data.checkid,
				activityCode: this.data.activityId,
				shopCode: getApp().globalData.common.shopCode,
			},
			header: getApp().globalData.header,
			success: function(res) {
				console.log(res)
				if (res.data.code == 200) {
					_self.setData({
						promLabel: res.data.obj.promLabel,
						shopName: res.data.obj.shopName
					})
					_self.showInfoAction();
				} else {


				}
			}
		})
	},

	checkRechargeInfo: function() {
		var _self = this;
		wx.request({
			url: getApp().globalData.baseUrl + '/applet/recharge/checkPromotionItem.do',
			data: {
				itemId: this.data.checkid,
				activityCode: this.data.activityId,
				shopCode: getApp().globalData.common.shopCode,
			},
			header: getApp().globalData.header,
			success: function(res) {
				console.log(res)
				if (res.data.code == 200) {
					console.log("*****")
					console.log(res.data.obj.result)
					if (res.data.obj.result == '1') {
						_self.payAction();
					} else if (res.data.obj.result == '2') {
						_self.hideView();
						_self.getActivityinfo(_self.data.activityId);
					} else {
						wx.redirectTo({
							url: '/pages/recharge/recharge?balance=' + _self.data.balance
						})
					}
				} else {

				}
			}
		})

	},
	payAction: function() {
		var _self = this;
		wx.request({
			url: getApp().globalData.baseUrl + '/applet/order/v1/createRechargeOrder.do',
			data: {
				itemId: this.data.checkid,
				activityCode: this.data.activityId,
				shopCode: getApp().globalData.common.shopCode,
				rechargeAmount: this.data.rechargeAmount
			},
			header: getApp().globalData.header,
			success: function(res) {
				console.log(res)
				_self.hideView();
				if (res.data.code == 200) {
					if (res.data.obj.type == 0) {
						//type=0有密码支付,未开通免密
						wx.requestPayment({
							'timeStamp': res.data.obj.timeStamp,
							'nonceStr': res.data.obj.nonceStr,
							'package': res.data.obj.prepay_id,
							'signType': res.data.obj.signType,
							'paySign': res.data.obj.paySign,
							'success': function(response) {
								wx.showToast({
									title: '充值成功,预计10分钟内到账',
									icon: 'none',
									duration: 2000,
									mask: true,
									'success': function() {
										setTimeout(() => {
											wx.switchTab({
												url: '../mine/mine'
											})
										}, 2000)
									}
								})

							},
							'fail': function(res) {
								console.log(res)
								wx.showToast({
									title: '取消支付',
									icon: 'none',
									duration: 1500
								})
							}
						})


					} else {

						if (res.data.obj.status == 1) {
							wx.showToast({
								title: '充值成功,预计10分钟内到账',
								icon: 'none',
								duration: 2000,
								mask: true,
								'success': function() {
									setTimeout(() => {
										wx.switchTab({
											url: '../mine/mine'
										})
									}, 2000)
								}
							})
						} else if (res.obj.status == 4) {
							wx.showToast({
								title: '支付中...请稍后查看余额明细',
								icon: 'none',
								duration: 2000,
								mask: true,
								'success': function() {
									setTimeout(() => {
										wx.switchTab({
											url: '../mine/mine'
										})
									}, 2000)
								}
							})
						} else {
							wx.showToast({
								title: '支付失败',
								icon: 'none',
								duration: 2000,
								mask: true,
								'success': function() {

								}
							})
						}


					}


				} else {
					wx.showToast({
						title: '支付失败',
						icon: 'none',
						duration: 2000,
						mask: true,
						'success': function() {

						}
					})

				}
			}
		})
	},
	chooseAction: function(e) {
		var code = e.currentTarget.dataset.code;
		var list = this.data.list;
		for (var i = 0; i < list.length; i++) {
			list[i].isSelect = 'nomal';
			if (code == list[i].itemId) {
				list[i].isSelect = 'select';
				this.setData({
					checkid: list[i].itemId,
					rechargeAmount: list[i].rechargeCondition
				})
			}
		}
		this.setData({
			list: list
		})

	},
	hideView: function() {

		this.setData({
			showFloat: false,
			showInfo: false
		})
	},
	showInfoAction: function() {
		this.setData({
			showFloat: true,
			showInfo: true
		})
	},
	rechageDetail: function(e) {
		var code = e.currentTarget.dataset.code;
		var _self = this;
		console.log(_self.data.activityId)
		wx.navigateTo({
			url: '/pages/comingCoupons/comingCoupons?code=' + code + '&id=' + _self.data.activityId
		})

	}
})